import React, { PureComponent, Fragment } from 'react';
import { connect } from 'dva';
import { Card, Input, Table, Divider, Row, Col, InputNumber, Button } from 'antd';
import PageHeaderLayout from '../../layouts/PageHeaderLayout';
import { routerRedux } from 'dva/router';
import ConfirmModal from '../../components/_public/ConfirmModal';
import LessonPreview from '../../components/_public/LessonPreview';

import { getParamUrl } from '../../utils/formatDataSource';

@connect(({ lesson3, loading }) => ({
  lesson:lesson3,
  loading: loading.models.lesson3,
}))
class NoMathLessonInfo extends PureComponent {
  state = {
    lessonId: 0,
    visible: false,
    previewShow: false,
    deleteType: 0, // 当前删除配置对应的类型
    content: [],
  };
  componentDidMount = () => {
    const { dispatch, location: { search } } = this.props;
    const params = getParamUrl(search);
    dispatch({
      type: 'lesson3/getLessonInfoDetail',
      payload: params,
    });
    dispatch({
      type: 'lesson3/lessonInfo',
      payload: params,
    });
    this.setState({
      lessonId: params.lessonId,
    });
  };

  studentColumn = () => {
    return [
      {
        title: '姓名',
        dataIndex: 'studentName',
        key: 'studentName',
      },
      {
        title: '年级',
        dataIndex: 'classLevel',
        key: 'classLevel',
      },
      {
        title: '性别',
        dataIndex: 'sex',
        key: 'sex',
      },
      {
        title: '上次作业成绩',
        key: 'lastScore',
        render: record => {
          if (record.lastScore === '--- --') {
            return (
              <InputNumber
                min={1}
                max={200}
                defaultValue={0}
                onChange={e => {
                  this.onChange(e, record);
                }}
              />
            );
          } else {
            return <span>{record.lastScore}</span>;
          }
        },
      },
      {
        title: '上次考勤',
        dataIndex: 'kq',
        key: 'kq',
      },
      {
        title: '操作',
        key: 'lastClock',
        render: item => {
          return (
            <a href="javascript:;" onClick={() => this.toDetailPage(item)}>
              查看
            </a>
          );
        },
      },
    ];
  };
  toDetailPage = item => {
    sessionStorage.setItem('lessonId', this.state.lessonId);
    sessionStorage.setItem('lessonType', 0);
    this.props.dispatch(routerRedux.push(`/studentManage/student/info?studentId=${item.id}`));
  };
  onChange = (e, item) => {
    item.lastScore = e;
  };
  submit = () => {
    const { studentInfo: { data } } = this.props.lesson;
    const userSave = [];
    data.map(d => {
      userSave.push({
        studentId: d.id,
        lastScore: d.lastScore === '--- --' ? 0 : d.lastScore,
      });
    });
    const requestParam = {
      lessonId: this.state.lessonId,
      data: userSave,
    };
    this.props
      .dispatch({
        type: 'lesson3/saveUnMathScore',
        payload: requestParam,
      })
      .then(() => {
        const { dispatch, location: { search } } = this.props;
        const params = getParamUrl(search);
        dispatch({
          type: 'lesson3/getLessonInfoDetail',
          payload: params,
        });
      });
  };
  getHidden = () => {
    const { studentInfo: { data } } = this.props.lesson;
    let hidden = true;
    data.map(d => {
      if (d.lastScore === '--- --') {
        hidden = false;
      }
    });
    return hidden;
  };

  render = () => {
    const { studentInfo = {}, lessonInfo = {} } = this.props.lesson;
    const { TextArea } = Input;

    return (
      <PageHeaderLayout>
        <Card bordered={false}>
          <Row>
            <Col span={6}>
              上课时间：<strong>{`${lessonInfo.lessonDate}  ${lessonInfo.timeFrom} - ${
                lessonInfo.timeTo
              }`}</strong>
            </Col>
            <Col span={3}>
              课程类型：<strong>{lessonInfo.typeName}</strong>
            </Col>
            <Col span={4}>
              课程状态：<strong>{lessonInfo.classStatusName}</strong>
            </Col>
            <Col span={11} style={{ textAlign: 'right' }}>
              <Button
                type="primary"
                size="small"
                onClick={() => this.props.dispatch(routerRedux.push('/lesson/time'))}
              >
                返回
              </Button>
            </Col>
          </Row>
        </Card>
        <Divider />
        <Card bordered={false} style={{ minHeight: '800px' }}>
          <span style={{ fontSize: '20px' }}>
            <strong>学生明细</strong>
          </span>
          <Table
            dataSource={studentInfo.data}
            columns={this.studentColumn()}
            loading={this.props.loading}
            bordered
            pagination={{ total: studentInfo.total, hideOnSinglePage: true }}
            size="small"
            style={{ marginTop: '20px', marginBottom: '20px' }}
          />
          <Button type="primary" onClick={this.submit} hidden={this.getHidden()}>
            提交
          </Button>
        </Card>
      </PageHeaderLayout>
    );
  };
}

export default NoMathLessonInfo;
